using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using pigs;

namespace Pigs.Enemies
{
    class MountainBoss : pigs.Pig
    {
        Boolean goingUp;// is the big going up or down on the screen
        HealthBar healthBar;
        GraphicsDevice graphics;

        public MountainBoss(Texture2D texture, Vector2 playerPosition, GraphicsDevice gd, int screenWidth)
            : base(texture, playerPosition)
        {
            START_SPEED = 0.75f;
            START_HEALTH = 100;
            this.health = 100;
            this.shouldEatSlop = true;
            this.ID = "mountainBoss";
            this.speed = START_SPEED;
            this.isInvincible = true;
            goingUp = true;

            this.graphics = gd;
            this.healthBar = new HealthBar(Color.Green, this.health, gd, new Vector2(50, 50), screenWidth);
        }

        /// <summary>
        /// move to the left until at 400px
        /// after that move up and down
        /// </summary>
        public void Update(List<Pigs.Weapons.Slop> slop)
        {
            int i = 0;
            foreach (Pigs.Weapons.Slop s in slop)
            {
                if (s.Active)
                {
                    if (Vector2.Distance(s.Position, new Vector2(this.position.X, (this.position.Y + this.texture.Height / 2))) < 300 && i < 1)
                    {
                        if (this.position.X < s.Position.X)
                        {
                            this.position.X += 0.5f;
                        }
                        if (this.position.X > s.Position.X)
                        {
                            this.position.X -= 0.5f;
                        }
                        if (this.position.Y + this.texture.Height / 2 > s.Position.Y)
                        {
                            this.position.Y -= 0.5f;
                        }
                        else if (this.position.Y + this.texture.Height / 2 < s.Position.Y)
                        {
                            this.position.Y += 0.5f;
                        }

                        if (Vector2.Distance(s.Position, new Vector2(this.position.X, (this.position.Y + this.texture.Height / 2))) < 100 && i < 1)
                        {
                            this.isEatingSlop = true;
                            s.scale -= .005f;
                            if (s.scale < 0.01f) s.Active = false;
                        }
                        i++;
                    }
                }
            }
            if (i == 0)
            {
                this.isEatingSlop = false;
                if (this.position.X > 400)
                {
                    this.position.X -= START_SPEED;
                }
                else if (goingUp)
                {
                    this.position.Y -= START_SPEED;
                    if (position.Y < 50)
                    {
                        goingUp = false;
                    }
                }
                else
                {
                    this.position.Y += START_SPEED;
                    if (position.Y > 718 - Height)
                    {
                        goingUp = true;
                    }
                }
            }
            rectangle = new Rectangle((int)position.X, (int)position.Y, Width, Height);
            healthBar.update(graphics, health);
        }

        /// <summary>
        /// draw the boss
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (healthBar.oldHealth > health)
            {
                spriteBatch.Draw(this.texture, this.position, null, Color.Red, 0f, Vector2.Zero, 1f, effect, 0f);
            }
            else
            {
                spriteBatch.Draw(this.texture, this.position, null, Color.White, 0f, Vector2.Zero, 1f, effect, 0f);
            }
            healthBar.Draw(spriteBatch);
        }
    }
}